module namespace getinfo = "http://www.xmlproject.com/smi/lib/controller/getinfo";
import module namespace rest = "http://expath.org/ns/http-client";
import module namespace json = "http://www.zorba-xquery.com/modules/json";
import module namespace base64 = "http://www.zorba-xquery.com/modules/base64";
import module namespace html = "http://www.zorba-xquery.com/modules/tidy";
declare namespace youNS="http://www.w3.org/2005/Atom";

declare sequential function getinfo:imdb($id as xs:string)
{
    let $searchstring := fn:encode-for-uri($id)
    let $imdbUrl := fn:concat("http://www.imdbapi.com/?i=", $searchstring, "&amp;r=XML") 
    
    return
        <searchresult xmlns:roy="http://www.roy.pe">
            <provider>IMDB</provider>
            {
                (: send request to google json api and parse the response :)
                let $hits := rest:send-request((), $imdbUrl)[2]
                for $hit in $hits//movie
                let $genre := string($hit/@genre)
                let $writer := string($hit/@writer)                
                let $director := string($hit/@director)
                let $actors := string($hit/@actors)
                
                (: we use tidy to remove the encoded html tags (e.g. &lt;) in the google contents :)
                return
                    <movie>
                        <genre>{$genre}</genre>                        
                        <writer>{$writer}</writer>
                        <director>{$director}</director>
                        <actors>{$actors}</actors>
                    </movie>                    
            }
        </searchresult>
};

declare sequential function getinfo:themoviedb($id as xs:string)
{
    let $searchstring := fn:encode-for-uri($id)
    let $theMovieDbUrl := fn:concat("http://api.themoviedb.org/2.1/Movie.getInfo/en/xml/3b95274f2f660c999668adef7b824dd8/", $searchstring) 
    
    return
        <searchresult xmlns:roy="http://www.castelo.pe">
            <provider>ThemovieDB</provider>
            {
                (: send request to google json api and parse the response :)
                let $hits := rest:send-request((), $theMovieDbUrl)[2]
                for $hit in $hits//movie
                let $overview := string($hit/overview)
                let $cover := if( $hit/images/image/@size = 'w342' )
                then  $hit/images/image[4]/@url
                else "nada"
               (: let $cover := $hit/images/image[1]/@url:)
                (: we use tidy to remove the encoded html tags (e.g. &lt;) in the google contents :)
                return
                    <movie>
                        <overview>{$overview}</overview>                        
                        <cover>{$cover}</cover>
                    </movie>                    
            }
        </searchresult>
};

declare sequential function getinfo:rotentomatoes($id as xs:string)
{
    let $searchstring := fn:encode-for-uri($id)
    let $rTomatoesUrl := fn:concat("http://api.rottentomatoes.com/api/public/v1.0/movies/", $searchstring, "..json?apikey=xakjcx3ztntapmd3fptsrtbs")
    
    return
        <searchresult xmlns:roy="http://www.castelo.pe">
            <provider>RotenTomatoes</provider>
            {
                (: send request to google json api and parse the response :)
                let $hits_json := rest:send-request((), $rTomatoesUrl)[2] 
                let $hits_xml := json:parse($hits_json)
                (:let $title := if ($hits_xml//pair/@name = 'title')
                then $hits_xml/jason/pair
                else "nada":)
                let $title := $hits_xml//pair[@name="title"]/text()
               (: let $cover := $hit/images/image[1]/@url:)
                (: we use tidy to remove the encoded html tags (e.g. &lt;) in the google contents :)
                return
                    <movie>
                        <title>{$title}</title>                        
                    </movie>                    
            }
        </searchresult>
};

declare sequential function getinfo:youtube($query as xs:string)
{
    let $searchstring := fn:encode-for-uri($query)
    let $youtubeURL := "http://gdata.youtube.com/feeds/api/videos?q="
    let $youtubeURL := fn:concat($youtubeURL, $searchstring)
    let $youtubeURL2 := "movie+trailer+Official&amp;orderby=viewCount&amp;start-index=1&amp;max-results=5&amp;v=2"
    let $youtubeURL := fn:concat($youtubeURL, $youtubeURL2)
  (: prepare the search url :)
  
  return 
    <searchresult>
            <provider>Youtube</provider>
            {
            (:string(doc("2youtube.xml")//entry/link[@rel = "alternate"]/@href):)
                let $hits := rest:send-request((), $youtubeURL)[2]
                for $hit in $hits//youNS:entry
                
                    let $title := string($hit//youNS:title)
                    let $href := string($hit//youNS:link[@rel = "alternate"]/@href)
                    let $content := string($hit//youNS:link[@rel = "alternate"]/@href)
                    let $rating := string($hit//youNS:link[@rel = "alternate"]/@href)
                    let $views := string($hit//youNS:link[@rel = "alternate"]/@href)
                    (: we use tidy to remove the encoded html tags (e.g. &lt;) in the google contents :)
                    let $content := html:tidy($href)
                return <result title="{$title}" href="{$href}">{$content}</result>
            }
        </searchresult>
};